﻿const cloud = require('wx-server-sdk')
const QcloudSms = require("qcloudsms_js")
const appid = *********** //替换成您申请的云短信 AppID 以及 AppKey
const appkey = "*******************************"//替换成您申请的云短信 AppID 以及 AppKey
const smsSign = ""
const uinSendNum = 3
cloud.init()
const db = cloud.database()

exports.main = async (event, context) => new Promise((resolve, reject) => {
  const wxContext = cloud.getWXContext()
  var {
    phone,
    templateId
  } = event
  console.log(phone, templateId)
  if (templateId == 386540) {
    db.collection('Verify').where({
      phone: phone
    }).orderBy('sendtime', 'desc').limit(uinSendNum).get().then(res => {
      if (res.data.length === 3 && new Date(res.data[2].sendtime).toDateString() === new Date().toDateString()) {
        console.log("uin超限: ")
        resolve({
          code: 1013,
          msg: '您频繁发送手机验证码，请稍后再试'
        })
      } else {
        let qcloudsms = QcloudSms(appid, appkey)
        let ssender = qcloudsms.SmsSingleSender()
        let verify = ('000000' + Math.floor(Math.random() * 999999)).slice(-6)
        let params = [verify]
        ssender.sendWithParam(86, event.phone, templateId,
          params, smsSign, "", "", (err, res, resData) => {
            if (err) {
              console.log("err: ", err);
              reject({
                err
              })
            } else {
              if (resData.result === 0) {
                var {
                  result,
                  errmsg,
                  fee,
                  sid
                } = resData
              } else {
                var {
                  result,
                  errmsg
                } = resData
              }
              db.collection('Verify').add({
                data: {
                  phone: event.phone,
                  verify: verify,
                  activetime: 5,
                  sendtime: new Date().getTime(),
                  senddate: new Date(),
                  fee: fee ? fee : '',
                  sid: sid ? sid : '',
                  sendResult: result,
                  errmsg: errmsg,
                  recvtime: '',
                  recvmsg: '',
                  recvresult: '',
                  inpuTime: '',
                  inputVerify: '',
                  inputCount: parseInt(0),
                  verifyStatus: false
                }
              }).then(res => {
                resolve({
                  code: result,
                  msg: result === 0 ? '验证码发送成功' : errmsg
                })
              })
            }
          }
        )
      }
    })
  }

  if (templateId == 386955) {
    console.log(templateId, phone)
    let qcloudsms = QcloudSms(appid, appkey)
    let ssender = qcloudsms.SmsSingleSender()
    let name = event.name
    let staff = event.staff
    let staffphone = event.staffphone
    let params = [name, staff, staffphone]
    ssender.sendWithParam(86, event.phone, templateId,
      params, smsSign, "", "", (err, res, resData) => {
        if (err) {
          console.log("err: ", err);
          reject({
            err
          })
        } else {
          if (resData.result === 0) {
            var {
              result,
              errmsg,
              fee,
              sid
            } = resData
          } else {
            var {
              result,
              errmsg
            } = resData
          }
        }
      }
    )
  }

  if (templateId == 386954) {
    console.log(templateId, phone)
    let qcloudsms = QcloudSms(appid, appkey)
    let ssender = qcloudsms.SmsSingleSender()
    let name = event.name
    let params = [name]
    ssender.sendWithParam(86, event.phone, templateId,
      params, smsSign, "", "", (err, res, resData) => {
        if (err) {
          console.log("err: ", err);
          reject({
            err
          })
        } else {
          if (resData.result === 0) {
            var {
              result,
              errmsg,
              fee,
              sid
            } = resData
          } else {
            var {
              result,
              errmsg
            } = resData
          }
        }
      }
    )
  }
})